﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guru.Web.Controls.Asp;
using System.IO;
using System.Web.UI;
using System.Collections.Specialized;
using Guru.Web.UI;

namespace Guru.Web.Controls.Asp
{    
    public class PlaceHolderToggle : PlaceHolder, IMultiLanguageLoader
    {
        private HiddenField _uxActiveStatus;
        private string _IsTempActive;

        public PlaceHolderToggle()
            : base()
        {
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (_uxActiveStatus == null)
            {
                _uxActiveStatus = new HiddenField();
                _uxActiveStatus.ID = string.Format("{0}_PanelToggle_ActiveStatus", this.ID);
                if (!string.IsNullOrEmpty(_IsTempActive))
                {
                    _uxActiveStatus.Value = _IsTempActive;
                    _IsTempActive = string.Empty;
                }
            }
            this.Controls.Add(_uxActiveStatus);
            this.LoadTextFromResource(((GuruPage)this.Page).CurrentLanguage);
        }

        public string PanelSlideClientID
        {
            get
            {
                return string.Format("{0}_SildePanel", this.ClientID);
            }
        }
        public string ToogleButtonClientID
        {
            get
            {
                return string.Format("{0}_ToogleButton", this.ClientID);
            }
        }
        public string ToggleText
        {
            get;
            set;
        }
        public bool IsActive
        {
            set
            {
                if (this._uxActiveStatus != null)
                {
                    this._uxActiveStatus.Value = value ? "1" : "0";
                    _IsTempActive = string.Empty;
                }
                else 
                {
                    _IsTempActive = value ? "1" : "0";
                }
            }
            get
            {
                if (this._uxActiveStatus != null && this._uxActiveStatus.Value.Trim() == "1")
                {
                    return true;
                }
                return false;
            }
        }
               
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (string.IsNullOrEmpty(_uxActiveStatus.Value))
            {
                _uxActiveStatus.Value = "0";
            }
            string clientScript = @"
                $(document).ready(function() {2} 
                    $('#{0}').click(function() {2}                        
                        $('#{1}').slideToggle('normal'); 
                        $('#{0}').toggleClass('active'); 
                        var uxActive = $('#{4}');
                        if(uxActive.val() == '1') uxActive.val('0');
                        else uxActive.val('1');
                        return false; 
                    {3});                    
                {3});
                ";
            clientScript = string.Format(clientScript, this.ToogleButtonClientID, this.PanelSlideClientID, "{", "}", this._uxActiveStatus.ClientID);
            this.Page.ClientScript.RegisterStartupScript(typeof(PlaceHolderToggle), string.Format("{0}_SildeEvent", this.ClientID), clientScript, true);
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            StringBuilder baseHTML = new StringBuilder();
            using (StringWriter twr = new StringWriter(baseHTML))
            {
                using (HtmlTextWriter newWriter = new HtmlTextWriter(twr))
                {
                    base.Render(newWriter);
                    newWriter.Flush();
                }
            }
            string display = this.IsActive ? string.Empty : "display:none";
            string activeCss = this.IsActive ? " active" : string.Empty;
            writer.Write(HTMLTemplate.GetByKey("Guru.Web.Controls.Asp.PlaceHolderToggle"), this.PanelSlideClientID, display, this.ToogleButtonClientID, activeCss, this.ToggleText, baseHTML);          
           
        }
        public void SetText(NameValueCollection text)
        {
            this.ToggleText = text[this.ID];
        }
    }
}
